<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <!-- <HighFormQuery :options="searchOptions" @submit="handleQuery" /> -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="期刊名称" prop="journalName">
        <el-input
          v-model="queryParams.journalName"
          placeholder="请输入期刊名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="期刊编号" prop="journalCode">
        <el-input
          v-model="queryParams.journalCode"
          placeholder="请输入期刊编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="ISSN/ISBN" prop="sn">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入ISSN/ISBN"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="journalFrom">
        <el-input
          v-model="queryParams.journalFrom"
          placeholder="请输入出版社"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <span v-show="isShow">
        <el-form-item label="语言" prop="language">
          <el-input
            v-model="queryParams.language"
            placeholder="请输入语言"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="出版频率" prop="frequency">
          <el-select
            v-model="queryParams.frequency"
            placeholder="请选择出版频率"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_FREQUENCY)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主题分类" prop="topicType">
          <el-select
            v-model="queryParams.topicType"
            placeholder="请选择主题分类"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="期刊发行时间" prop="publishYear">
          <el-date-picker
            v-model="queryParams.publishYear"
            type="year"
            value-format="YYYY"
            placeholder="请选择期刊发行时间"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="期刊来源" prop="source">
          <el-select
            v-model="queryParams.source"
            placeholder="请选择期刊来源"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_SOURCE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="期刊状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择期刊状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.JOURNAL_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </span>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="text" @click="isShow = !isShow">{{ isShow ? '收缩' : '展开'}}</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="#" align="center" type="index" width="50" fixed="left" />
      <el-table-column label="封面" align="center" prop="mainImage">
        <template #default="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.mainImage"
            :preview-src-list="[scope.row.mainImage]"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column label="期刊名称" align="center" prop="journalName" />
      <el-table-column label="期刊编号" align="center" prop="journalCode" />
      <el-table-column label="ISSN/ISBN" align="center" prop="sn" min-width="100px"/>
      <el-table-column label="出版社" align="center" prop="journalFrom" />
      <el-table-column label="语言" align="center" prop="language" />
      <el-table-column label="出版频率" align="center" prop="frequency">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_FREQUENCY" :value="scope.row.frequency" /> 
        </template>
      </el-table-column>
      <el-table-column label="主题分类" align="center" prop="topicType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_TYPE" :value="scope.row.topicType" /> 
        </template>
      </el-table-column>
      <el-table-column label="期刊发行时间" align="center" prop="publishYear" />
      <el-table-column label="期刊来源" align="center" prop="source">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_SOURCE" :value="scope.row.source" />
        </template> 
      </el-table-column>
      <!-- <el-table-column label="订购来源" align="center" prop="orderSource" /> -->
      <!-- <el-table-column
        label="订购日期"
        align="center"
        prop="orderTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <el-table-column
        label="入库时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="期刊状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JOURNAL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="60px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openDetail(scope.row)"
            v-hasPermi="['dr:journal:query']"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 详情 -->
  <JournalDetail v-model:visible="detailVisible" :data="currentRow" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { JournalApi, JournalVO } from '@/api/dr/journal'

/** 期刊信息 列表 */
defineOptions({ name: 'Journal' })
const loading = ref(true) // 列表的加载中
const list = ref<JournalVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  journalName: undefined,
  journalCode: undefined,
  sn: undefined,
  journalFrom: undefined,
  language: undefined,
  frequency: undefined,
  file: undefined,
  volumeNumber: undefined,
  topicType: undefined,
  issue: undefined,
  publishYear: undefined,
  pageRange: undefined,
  source: undefined,
  status: undefined,
  orderSource: undefined,
  position: undefined,
  webUrl: undefined,
  createTime: undefined,
})
const searchOptions = ref([
  {
    label: '期刊名称',
    value:'journalName',
  },
  {
    label: '期刊编号',
    value:'journalCode',
  },
  {
    label: 'ISSN/ISBN',
    value:'sn',
  },
  {
    label: '出版社',
    value:'journalFrom',
  },
  {
    label: '语言',
    value:'language',
  },
  {
    label: '出版频率',
    value:'frequency',
  },
  {
    label: '主题分类',
    value:'type',
  },
  {
    label: '卷号',
    value:'volumeNumber',
  },
  {
    label: '期号',
    value:'issue',
  },
  {
    label: '出版年份',
    value:'publishYear',
  },
  {
    label: '页码范围',
    value:'pageRange',
  },
  {
    label: '期刊来源',
    value:'source',
  },
  {
    label: '状态',
    value:'status',
  },
  {
    label: '订购来源',
    value:'orderSource',
  },
  {
    label: '订购日期',
    value:'orderTime',
  },
  {
    label: '存放位置',
    value:'position',
  },
  {
    label: '入库时间',
    value:'createTime',
  },
  {
    label: '更新时间',
    value:'updateTime',
  },

])
const isShow = ref<Boolean>(false)
const detailVisible = ref<boolean>(false)
const currentRow = ref<JournalVO>()
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JournalApi.getJournalPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = (params = {}) => {
  console.log('handleQuery params', params)
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

// 详情弹窗
const openDetail = (row: any) => {
  currentRow.value = { ...row }
  detailVisible.value = true
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>